package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.bean.MyPage;
import com.itheima.result.R;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    //注入service层得对象
    @Autowired
    private CategoryService categoryService;
    //接受添加请求
    @PostMapping
    public R add(@RequestBody Category category/*, HttpSession session*/){
        /*Employee em = (Employee) session.getAttribute("employee");
        System.out.println(category);
        //调用service层得添加方法添加数据
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(em.getId());
        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(em.getId());*/
        int row = categoryService.add(category);
        if (row>0){
            return  R.success("添加成功！");
        }else{
            return  R.error("添加失败！");
        }

    }

    @GetMapping("/page")
    public R findPage(MyPage page){
        //调用service层的方法，进行分页查询
        IPage<Category> p = categoryService.findPage(page);
        //返回查询数据
        return R.success(p);
    }

    @DeleteMapping
    public R delete(long id){
        //调用service层的方法进行删除操作
        int row = categoryService.delete(id);
        if (row>0){
            return  R.success("删除成功！");
        }else{
            return  R.error("删除失败！");
        }
    }

    @PutMapping
    public R update(@RequestBody Category category){
        int row = categoryService.update(category);
        if (row>0){
            return  R.success("更新成功！");
        }else{
            return  R.error("更新失败！");
        }
    }

    @GetMapping("/list")
    public R findType(Integer type){
        List<Category> list = categoryService.findType(type);
        return R.success(list);
    }
}
